package com.jdrx.sw.sewage.service.common;

import com.alibaba.fastjson.JSONObject;
import com.jdrx.sw.sewage.beans.entity.StationPO;
import com.jdrx.sw.sewage.beans.vo.DeviceIotVO;
import com.jdrx.sw.sewage.beans.vo.ProcessunitVO;
import com.jdrx.sw.sewage.beans.vo.StorageDeviceVO;
import com.jdrx.sw.sewage.common.enums.StorageDevType;
import com.jdrx.sw.sewage.dao.DevStorageDAO;
import com.jdrx.sw.sewage.dao.DeviceDAO;
import com.jdrx.sw.sewage.dao.ProcessunitDAO;
import com.jdrx.sw.sewage.dao.StationDAO;
import com.jdrx.sw.sewage.service.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import static com.jdrx.sw.sewage.common.constant.Constant.MQ_DEVICE_KEY;
import static com.jdrx.sw.sewage.common.constant.Constant.USE_DEVICE_KEY;

@Service
public class CacheService extends BaseServiceImpl {
    public static final Logger logger = LoggerFactory.getLogger(CacheService.class);

    @Autowired
    private ProcessunitDAO processunitDAO;
    @Autowired
    private DeviceDAO deviceDAO;
    @Autowired
    private DevStorageDAO devStorageDAO;
    @Autowired
    private RedisService redisService;
    @Autowired
    private StationDAO stationDAO;

    public void selectStorageByStationIds() {
        Map<String, Object> map = new HashMap<>();
        List<StationPO> stationPOS = stationDAO.selectStationList(null);
        stationPOS.forEach(station -> {
            Long stationId = station.getId();
            AtomicInteger storageDeviceNum = new AtomicInteger(0);
            List<ProcessunitVO> sPList = processunitDAO.selectByStationId(stationId);
            sPList.stream().forEach(item -> {
                List<StorageDeviceVO> storageDeviceVOS = deviceDAO.selectByStIdAndPId(stationId, item.getId());
                storageDeviceVOS.forEach(dev -> {
                    dev.setAttachmentNum(devStorageDAO.selectAttachmentNum(StorageDevType.ATTACHMENT.getKey(), dev.getDossierSpec(), dev.getCategoryId(), dev.getMaterielId()));
                });
                map.put(item.getName(), storageDeviceVOS);
                storageDeviceNum.set(storageDeviceNum.get() + storageDeviceVOS.size());
            });
            map.put("storageDeviceNum", storageDeviceNum);
            redisService.setJson(USE_DEVICE_KEY + stationId, new JSONObject(map));
        });
    }

    public void selectMQDevices() {
        // 组装解析后的值存入redis
        List<DeviceIotVO> devices = deviceDAO.selectMQDevices(null);
        if (null != devices && devices.size() > 0) {
            devices.forEach(item -> {
//                DeviceIotVO deviceIotVO = redisService.getJson(MQ_DEVICE_KEY + item.getIotGatewayNum() + "-" + item.getGatherDevPointValue(), DeviceIotVO.class);
                redisService.setJson(MQ_DEVICE_KEY + item.getIotGatewayNum() + "-" + item.getGatherDevPointValue(), item);
            });
        }
    }
}
